package com.ccys.common.third.payPal;


import com.ccys.common.third.payPal.config.*;

import com.paypal.api.payments.Links;
import com.paypal.api.payments.Payment;
import com.paypal.base.rest.PayPalRESTException;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.math.BigDecimal;

@Slf4j
@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = {PayPalConstants.class})
public class PayPalUtilTest {

       /**
        * @description 测试生成支付参数
        * @date     2024/3/6
        * @author   cloud fly
        */
    @Test
    public  void payTest()  {
        System.out.println("支付方式=" + PaypalPaymentMethod.paypal.toString());
        System.out.println("货币=" + PaypalCurrency.UNITED_STATES_DOLLAR.getCode());
        BigDecimal payAmount = new BigDecimal(1);
        Payment payment = null;
        try {
            payment = PayPalUtil.createPayment(
                payAmount,
                "USD",
                PaypalPaymentMethod.paypal,
                PaypalPaymentIntent.sale,
                "购买商品信息~",
                PayPalConstants.PAYPAL_CANCEL_URL,
                PayPalConstants.PAYPAL_SUCCESS_URL);
        } catch (PayPalRESTException e) {
            e.printStackTrace();
        }
        System.out.println("测试支付="+payment.toString());
        for (Links links : payment.getLinks()) {
            if (links.getRel().equals("approval_url")) {
                log.info("实际返回正确的地址=" + links.getHref());
            }
        }
    }
       /**
        * @description 测试退款
        * @date     2024/3/6
        * @author   cloud fly
        */
    @Test
    public  void payRefund()  {
        PaypalRefundRequest req = new PaypalRefundRequest();
        req.setCurrency("USD");
        //测试为1
        req.setAmount(new BigDecimal("1"));
        req.setReason("测试退款1美元");
        //交易单号
        req.setTradeNo("PAYID-MXTIWOY72M628182G8086024");
        PaypalRefundReturn refund = PayPalUtil.refundSale(req);
        //PaypalRefundReturn(currency=USD, amount=1.00, state=completed)
        System.out.println(refund);
    }


       /**
        * @description 测试查询支付订单信息
        * @date     2024/3/6
        * @author   cloud fly
        */
    @Test
    public  void getPaymentTest()  {
        //Payment payment = PayPalUtil.getPayment("PAYID-MXT5FIA0KF6448110438115U");
        Payment payment = PayPalUtil.getPayment("PAYID-MXTIWOY72M628182G8086024");
        System.out.println("返回参数 = " + payment);
    }


    @Test
    public  void getAccessToken()  {
        String accessToken = PayPalUtil.getAccessToken();
        System.out.println("获取访问token = " + accessToken);
    }






}
